package com.aurora.supermarket;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.aurora.supermarket.base.AppBaseActivity;
import com.aurora.supermarket.home.HomeFragment;
import com.aurora.supermarket.mycenter.MyCenterFragment;
import com.aurora.supermarket.presenter.MainPresenter;
import com.aurora.supermarket.view.MainActivityView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppBaseActivity<MainPresenter> implements MainActivityView {

    public static final String TAG = "MainActivity";
    private ViewPager vp_viewpager;
    private TabLayout tl_tabs;
    private List<Fragment> mfragments;
    private List<String> title;
    private MainFragmentAdapter adapter;
    private View view;
    private TextView tv_title;
    private ImageView img_title;
    FrameLayout fl_navigationbar;
    TextView action_bar_title;

    private int[] tabIcons = {
            R.mipmap.foot_home,
            R.mipmap.foot_class,
            R.mipmap.foot_scan,
            R.mipmap.foot_shopping,
            R.mipmap.foot_my
    };

    private int[] tabIconsPressed ={
            R.mipmap.foot_home_d,
            R.mipmap.foot_class_d,
            R.mipmap.foot_scan_d,
            R.mipmap.foot_shopping_d,
            R.mipmap.foot_my_d,
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        basePresenter.requestData();
        initToolbar(false,null,false);
        init();
    }

    private void init() {
        initView();
        initValue();
        initEvent();
    }

    private void initView() {
        action_bar_title = findViewById(R.id.action_bar_title);
        fl_navigationbar = findViewById(R.id.fl_navigationbar);
        vp_viewpager = findViewById(R.id.vp_viewpager);
        tl_tabs = findViewById(R.id.tl_tabs);

    }

    private void initValue() {
        mfragments = new ArrayList<>();
        mfragments.add(new HomeFragment());
        mfragments.add(new HomeFragment());
        mfragments.add(new HomeFragment());
        mfragments.add(new HomeFragment());
        mfragments.add(new MyCenterFragment());

        title = new ArrayList<>();
        title.add("首页");
        title.add("分类");
        title.add("扫一扫");
        title.add("购物车");
        title.add("我的");

        adapter = new MainFragmentAdapter(getSupportFragmentManager(),mfragments,title);
        vp_viewpager.setAdapter(adapter);
        vp_viewpager.setOffscreenPageLimit(5);
        tl_tabs.setupWithViewPager(vp_viewpager);
        setupTabIcons();
    }

    private void setupTabIcons() {
        for(int i=0;i<title.size();i++){
            TabLayout.Tab tab = tl_tabs.getTabAt(i);
            tab.setCustomView(getTabView(i));

        }
    }

    private View getTabView(int position) {
        view = LayoutInflater.from(this).inflate(R.layout.item_tablayout,null);
        tv_title = view.findViewById(R.id.tv_title);
        img_title = view.findViewById(R.id.img_title);
        tv_title.setText(title.get(position));
        img_title.setImageResource(tabIcons[position]);
        tv_title.setTextColor(getResources().getColor(R.color.black));
        view.setPadding(0,5,0,5);
        if(position==0){
            tv_title.setTextColor(getResources().getColor(R.color.baseColor));
            img_title.setImageResource(tabIconsPressed[position]);
        }
        return view;
    }

    private void initEvent() {
        tl_tabs.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                setTabSelected(tab);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                setTabUnselected(tab);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        vp_viewpager.setCurrentItem(2);
        tl_tabs.getTabAt(2).select();

    }

    private void setTabSelected(TabLayout.Tab tab) {
        View view = tab.getCustomView();
        TextView tv = view.findViewById(R.id.tv_title);
        tv.setTextColor(getResources().getColor(R.color.baseColor));
        ImageView iv = view.findViewById(R.id.img_title);
        iv.setImageResource(tabIconsPressed[tab.getPosition()]);
    }

    private void setTabUnselected(TabLayout.Tab tab) {
        View view = tab.getCustomView();
        TextView tv = view.findViewById(R.id.tv_title);
        tv.setTextColor(getResources().getColor(R.color.black));
        ImageView iv = view.findViewById(R.id.img_title);
        iv.setImageResource(tabIcons[tab.getPosition()]);
    }

    @Override
    public void onBackPressed() {
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
        alertDialog
                .setTitle("提示")
                .setMessage("是否退出应用!")
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        MainActivity.super.onBackPressed();
                    }
                });
        alertDialog.create().show();
    }

    @Override
    public MainPresenter attach() {
        return new MainPresenter(this);
    }

}
